package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class CourseController {
    @Autowired
    private CourseService courseService;

    //1.课程列表查询
    @GetMapping("/courses")
    public Result list(String name, Integer subject, Integer target,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        System.out.println("列表查询    " + name + "  " + subject + "  " + target + "  " + page + "  " + pageSize);
        PageResult<Course> pageResult = courseService.list(name, subject, target, page, pageSize);
        return Result.success(pageResult);
    }
    //2.删除课程
    @DeleteMapping("/courses/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除课程  " + id);
        courseService.delete(id);
        return Result.success();
    }
    //3.添加课程
    @PostMapping("/courses")
    public Result add(@RequestBody Course course) {
        log.info("添加课程  " + course);
        courseService.add(course);
        return Result.success();
    }
    //4.根据ID查询课程信息
    @GetMapping("/courses/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询课程信息  " + id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }
    //5.修改课程信息
    @PutMapping("/courses")
    public Result update(@RequestBody Course course) {
        log.info("修改课程信息  " + course);
        courseService.update(course);
        return Result.success();
    }
    //6.获取所有课程信息
    @GetMapping("/courses/list")
    public Result listAll() {
        return Result.success(courseService.listAll());
    }
    //7.根据学科查询课程信息
    @GetMapping("/courses/subject/{subject}")
    public Result listBySubject(@PathVariable Integer subject) {
        return Result.success(courseService.listBySubject(subject));
    }
}
